module dataLoader #(
    parameter Index = 0
)(
    input wire clk,
    input wire rst,
    input wire special_arbition,          //专用RAM的写仲裁结果信号
    input wire [15:0] special_idle,
    input wire common_arbition,            //组内通用RAM的写仲裁结果信号
    input wire [15:0] sram_write_en,
    input wire [3:0] group_memory_pick,         //RAM选择信号
    input wire [3:0] group_address_ready,       //RAM就绪信号
    input wire [15:0] address_ready,
    input wire [9:0] seed,
    output reg data_ready,
    output reg [31:0] data,               //数据
    output reg [5:0] data_priority,
    output reg [15:0] data_target_port,
    output reg data_write_en,
    output reg [9:0] data_size,
    output reg [5:0] group_priority,
    output reg [15:0] group_target_port,
    output reg group_write_en,
    output reg [9:0] group_size,
    output reg group_data_ready,
    output reg [31:0] group_data
);  

    reg [3:0] history_memory_pick;
    reg [9:0] temp_size;
    reg [2:0] work_state; //工作状�??
    reg [5:0] temp_port;
    reg [9:0] history_size;
    reg [5:0] history_priority;
    reg [15:0] history_target_port;
    integer dictHandler, fileHandler, i; // 文件句柄
    reg [175:0] line;                   // 用于存储每行读取内容
    reg [175:0] lines [0:511];
    reg [15:0] memory [0:1023];
    string line_name;
    string full_file_path;
    string dict_path;
    string work_path = "D:/work/0/in/";
    string record [0:511]; // 字符串数组，用于存储记录
    integer record_count, now_dict_index, now_file_index; // 记录计数�?
    
    always @(posedge clk) begin
        if (rst) begin
            //初始化信号
            data_write_en = 0;
            data_priority = 0;
            data_target_port = 0;
            data_ready = 0;
            
            group_write_en = 0;
            group_priority = 0;
            group_target_port = 0;
            group_data_ready = 0;
            data_ready = 0;
            work_path = $sformatf("C:/Users/hias-315/Desktop/HighSpeedFlow/work/%0d/in/", Index);
            work_state = 2'b000;
            now_dict_index = 0;
            // 打开文件
            dict_path = $sformatf("C:/Users/hias-315/Desktop/HighSpeedFlow/work/%0d/in/dict.bin", Index);
            dictHandler = $fopen(dict_path, "r");
            if (dictHandler == 0) begin
                $display("Error: Unable to open file 'dict.bin'");
                $finish;
            end
            record_count = 0;
            // 逐行读取文件
            while ($fgets(line, dictHandler)) begin
                if (line[3:0] != 4'b1010) begin
                    //把目录文件中的文件名称全部转存到寄存器数组备�?
                    lines[record_count] = line;
                    record_count = record_count + 1;
                end
            end
            // 关闭文件
            $fclose(dictHandler);
            //读取文件内容
            full_file_path = $sformatf("C:/Users/hias-315/Desktop/HighSpeedFlow/work/%0d/in/%s", Index, lines[now_dict_index]);
            now_dict_index = now_dict_index + 1;
            $readmemh(full_file_path, memory);
            now_file_index = 5;
            history_size = memory[0];
            history_target_port = memory[1];
            history_priority = memory[3][2:0];
        end
        else begin
            if (work_state == 2'b000) begin
                //如果对应的专用RAM处于空闲状�?�，发起仲裁信号
                if ((special_idle & history_target_port) > 0) begin
                    data_write_en=1;
                    data_priority=history_priority;
                    data_target_port=history_target_port;
                    data_size=history_size;
                    work_state=3'b001;

                    group_write_en=0;
                    group_priority=0;
                    group_target_port=0;
                    group_data_ready=0;
                end
                
                //如果对应的专用RAM处于繁忙状�?�，则发起对于�?�用RAM的仲�?
                if ((special_idle & history_target_port) == 0) begin
                    data_write_en=0;
                    data_priority=0;
                    data_target_port=0;

                    //向组管理器发出写请求
                    group_write_en=1;
                    group_priority=history_priority;
                    group_target_port=history_target_port;
                    group_size=history_size;
                    work_state=3'b101;
                end
            end
            else if (work_state == 2'b001) begin
                if ((sram_write_en&history_target_port)>0) begin
                    //请求成功，进入下�?阶段
                    if (special_arbition==1) begin
                        work_state = 3'b010;
                    end

                    //请求失败，进入组管理器发送阶�?
                    if (special_arbition==0) begin
                        data_write_en = 0;
                        data_priority = 0;
                        data_target_port = 0;
                        //向组管理器发出写请求
                        group_write_en = 1;
                        group_priority = history_priority;
                        group_target_port = history_target_port;
                        group_size = history_size;
                        work_state = 3'b101;
                    end
                end
            end    
            else if (work_state == 2'b101) begin
                if (common_arbition==0) begin
                    if ((special_idle&history_target_port)>0) begin
                        data_write_en = 1;
                        data_priority = history_priority;
                        data_target_port = history_target_port;
                        data_size = history_size;
                        work_state = 3'b001;

                        group_write_en = 0;
                        group_priority = 0;
                        group_target_port = 0;
                        group_data_ready = 0;
                    end
                end
                else if (common_arbition==1) begin
                    if (group_memory_pick>0) begin
                        history_memory_pick = group_memory_pick;
                        work_state = 3'b110;
                    end
                end
            end
            else if (work_state == 2'b010) begin
                if ((address_ready & history_target_port)>0) begin
                    temp_size = history_size;
                    //�?始传输数�?
                    data_write_en = 0;
                    data_priority = 0;
                    data_target_port = 0;
                    work_state = 3'b011;
                end
            end
            else if (work_state == 3'b110) begin
                if ((group_address_ready & history_memory_pick)>0) begin
                    temp_size = history_size;
                    //�?始传输数�?
                    group_write_en = 0;
                    group_priority = 0;
                    work_state = 3'b111;
                end
            end
            else if (work_state == 3'b111) begin
                if (temp_size == 1) begin
                    //�?后一个字的数据发送完毕，修改工作状�?�为空闲，可以处理其他内�?
                    work_state = 3'b000;
                    group_data_ready = 1'b0;
                    group_data = 0;
                    //读取文件内容
                    full_file_path = $sformatf("C:/Users/hias-315/Desktop/HighSpeedFlow/work/%0d/in/%s", Index, lines[now_dict_index]);
                    now_dict_index = now_dict_index + 1;
                    $readmemh(full_file_path, memory);
                    now_file_index = 5;
                    history_size = memory[0];
                    history_target_port = memory[1];
                    history_priority = memory[3][2:0];
                end
                else begin
                    //继续发�?�数�?
                    group_data_ready = 1'b1;
                    group_data = {memory[now_file_index], memory[now_file_index+1]};
                    now_file_index <= now_file_index + 2;
                end

                temp_size = temp_size - 1;
            end
        end
    end
endmodule